<?php
namespace App\Services;

use Illuminate\Support\Facades\DB;

class BaseService
{
    /**
     * 服务类，写复杂的逻辑
     */

    protected $db; //表对象
    //通用
    protected $dbAdmin = 'admin';
    protected $dbAdmLog = 'admin_login';
    protected $dbUser = 'user_wx';
    //码
    protected $dbQr = 'qrcode'; //码值
    protected $dbQrlog = 'qr_log'; //扫码记录
    protected $dbScene = 'scene'; //场景
    protected $dbSceneAttr = 'scene_attr'; //场景属性
    protected $dbSceneCnt = 'scene_cnt'; //场景内容
    protected $dbArea = 'area'; //省市区
    protected $dbVillage = 'village'; //小区
    protected $dbImg = 'image'; //图库
    protected $dbH5xcx = 'h5_xcx'; //H5到小程序
    //业务模板
    protected $dbTmp = 'tmp';
    protected $dbTmpAttr = 'tmp_attr';
    protected $dbTmpReply = 'tmp_reply';
    protected $dbTmpFollow = 'tmp_follow';
    //服务
    protected $dbFuwu = 'fuwu'; //第三方服务
    protected $dbFwCnt = 'fuwu_cnt'; //服务内容
    //广告推广
    protected $dbAdList = 'ad_list'; //广告队列
    protected $dbAdUsed = 'ad_used'; //广告已用

    /**
     * 场景内容类型
     * 1文字，2图片，3视频
     */
    protected $scene_types = [
        1 => '文字', '图片', '视频',
    ];
    //服务分类
    protected $fuwu_types = [
        1 => '家政蒸洗', '软件开发', '剪辑合成',
    ];

    //通过父级获取地区
    public function areaList()
    {
        $res = $this->dbConn($this->dbArea)
            ->where('pid', 0)
            ->get();
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
            ];
            $childs = $this->dbConn($this->dbArea)
                ->where('pid', $v['id'])
                ->get();
            if (count($childs)) {
                foreach ($childs as $vv) {
                    $vv = (array) $vv;
                    $arys[] = [
                        'id' => $vv['id'],
                        'title' => $v['title'].'-'.$vv['title'],
                    ];
                    $child2s = $this->dbConn($this->dbArea)
                        ->where('pid', $vv['id'])
                        ->get();
                    if (count($child2s)) {
                        foreach ($child2s as $vvv) {
                            $vvv = (array) $vvv;
                            $arys[] = [
                                'id' => $vvv['id'],
                                'title' => $v['title'].'-'.$vv['title'].'-'.$vvv['title'],
                            ];
                        }
                    }
                }
            }
        }
        return $arys;
    }

    //通过父级获取地区
    public function areaListByPid($pid)
    {
        return $this->dbConn($this->dbArea)
            ->where('pid', $pid)
            ->get();
    }

    //连接数据库
    public function dbConn($db)
    {
        return DB::connection('mysql')->table($db);
    }
}